﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;

namespace CMS_GYM.UserInterface
{
    public partial class ucFullAddress : UserControl
    {
        public int ID_Duong;
        public string Ten_Duong;
        public int ID_TP;
        public string Ten_TP;
        public int ID_Quan;
        public string Ten_Quan;
        public int ID_Phuong;
        public string Ten_Phuong;
        public string DiaChi;

        public string LINK_SERVER;
        
        public string CaptionText
        {
            get { return this.lblCaption.Text; }
            set { this.lblCaption.Text = value; }
        }

        public ucFullAddress()
        {
            InitializeComponent();
            this.link_LoadAddressInfo.TabStop = false;
            this.Load += new EventHandler(ucAddress_Load);
            this.link_LoadAddressInfo.Click += new EventHandler(link_LoadAddressInfo_Click);

            #region khai bao event address
            this.cbProvince1.cbProv.SelectedValueChanged += new EventHandler(cbProv_SelectedValueChanged);
            this.cbDistrict1.cbDist.SelectedValueChanged += new EventHandler(cbDist_SelectedValueChanged);
            this.cbStreet1.cbStrtName.SelectedValueChanged += new EventHandler(cbStrtName_SelectedValueChanged);
            this.cbWard1.cbWardName.SelectedValueChanged += new EventHandler(cbWardName_SelectedValueChanged);
            this.txtAddNo.TextChanged += new EventHandler(txtAddNo_TextChanged);
            this.txtAddNo.Click += new EventHandler(txtAddNo_Click);
            #endregion
        }

        void link_LoadAddressInfo_Click(object sender, EventArgs e)
        {
            if (LINK_SERVER != "")
                CapNhatThongTinDuong(LINK_SERVER);
        }        

        void txtAddNo_Click(object sender, EventArgs e)
        {
            this.txtAddNo.SelectAll();
        }
        public void cbWardName_SelectedValueChanged(object sender, EventArgs e)
        {
            try
            {
                if (cbWard1.cbWardName.SelectedValue != null)
                {
                    ID_Phuong = Convert.ToInt32(cbWard1.cbWardName.SelectedValue.ToString());
                    Ten_Phuong = cbWard1.cbWardName.Text;
                }
            }
            catch (Exception) { }
        }

        public void cbStrtName_SelectedValueChanged(object sender, EventArgs e)
        {
            try
            {
                if (cbStreet1.cbStrtName.SelectedValue != null)
                {
                    ID_Duong = Convert.ToInt32(cbStreet1.cbStrtName.SelectedValue.ToString());
                    Ten_Duong = cbStreet1.cbStrtName.Text;
                }
            }
            catch (Exception) { }
        }

        void txtAddNo_TextChanged(object sender, EventArgs e)
        {
            DiaChi = txtAddNo.Text;
        }

        void ucAddress_Load(object sender, EventArgs e)
        {
            //setAdress();
        }

        public void LoadData()
        {
            cbProvince1.LoadData();
            cbDistrict1.LoadData();
            cbWard1.LoadData();
            cbStreet1.LoadData();
            setAdress();
        }

        public void cbDist_SelectedValueChanged(object sender, EventArgs e)
        {
            try
            {
                if (cbDistrict1.cbDist.SelectedValue != null)
                {
                    ID_Quan = Convert.ToInt32(cbDistrict1.cbDist.SelectedValue.ToString());
                    Ten_Quan = cbDistrict1.cbDist.Text;
                }
                cbWard1.setDistrict(cbDistrict1.Dist_ID,LINK_SERVER);
            }
            catch (Exception) { }
        }
        public void cbProv_SelectedValueChanged(object sender, EventArgs e)
        {
            try
            {
                if (cbProvince1.cbProv.SelectedValue != null)
                {
                    try
                    {
                        ID_TP = Convert.ToInt32(cbProvince1.cbProv.SelectedValue.ToString());
                        Ten_TP = cbProvince1.cbProv.Text;
                    }
                    catch (Exception) { }
                }
                cbDistrict1.setProvince(cbProvince1.Prov_ID,LINK_SERVER);                
                if (cbDistrict1.cbDist.SelectedIndex == -1)
                    cbWard1.cbWardName.SelectedIndex = -1;
            }
            catch (Exception) { }
        }
        public void setAdress()
        {
            txtAddNo.Text = "Nhập số nhà";
            txtAddNo.SelectAll();
            //cbStreet1.cbStrtName.Text = "Chọn đường";
            //cbProvince1.cbProv.Text = "Chọn Thành Phố";
            //cbDistrict1.cbDist.Text = "Chọn Quận";
            //cbWard1.cbWardName.Text = "Chọn Phường";
            Ten_Duong = "";
            Ten_TP = "Hồ Chí Minh";
            Ten_Quan = "";
            Ten_Phuong = "";            
        }
        public void setAdress(string tp, string quan)
        {
            txtAddNo.Text = "Nhập số nhà";
            txtAddNo.SelectAll();
            cbStreet1.cbStrtName.Text = "Chọn đường";
            this.setTinhThanh(tp);
            this.setQuan(quan);
            Ten_Duong = "";
            Ten_TP = cbProvince1.cbProv.Text; ;
            Ten_Quan = cbDistrict1.cbDist.Text;
            Ten_Phuong = cbWard1.cbWardName.Text;
        }
        public void setSoNha(string sonha)
        {
            txtAddNo.Text = sonha;
        }
        public void setDuong(string id)
        {
            cbStreet1.SetStreet(id);
        }
        public void setPhuong(string id)
        {
            cbWard1.SetWard(id);
        }
        public void setQuan(string id)
        {
            cbDistrict1.SetDistrict(id);
        }
        public void setTinhThanh(string id)
        {
            cbProvince1.SetProvince(id);
        }
        public void setTinhThanh()
        {
            cbProvince1.cbProv.Text = "Hồ Chí Minh";
        }
        public void setQuanHuyen(string text)
        {
            cbDistrict1.cbDist.Text = text;
        }
        public void setTP(string text)
        {
            cbProvince1.cbProv.Text = text;
        }
        public void setEnableTP(bool b)
        {
            cbProvince1.Enabled = b;
        }
        public void setEnableQuan(bool b)
        {
            cbDistrict1.Enabled = b;
        }

        public string getFullAddress()
        {
            try
            {
                string str = string.Empty;
                str += string.IsNullOrEmpty(DiaChi) || DiaChi== "Nhập số nhà" ? string.Empty : DiaChi;
                str += string.IsNullOrEmpty(Ten_Duong) ? string.Empty : " " + Ten_Duong;
                str += string.IsNullOrEmpty(Ten_Phuong) ? string.Empty : ", " + Ten_Phuong;
                str += string.IsNullOrEmpty(Ten_Quan) ? string.Empty : ", " + Ten_Quan;
                str += string.IsNullOrEmpty(Ten_TP) ? string.Empty :  ", " + Ten_TP;
                return str;
            }catch(Exception ex)
            {
                return string.Empty;
            }
        }

        public void setFullAddress(string sHomeNumber, string sStreetID, string sWardID, string sDistrictID, string sProvID)
        {
            this.setSoNha(sHomeNumber);
            this.setDuong(sStreetID);
            this.setPhuong(sWardID);
            this.setQuan(sDistrictID);
            this.setTinhThanh(sProvID);
        }

        protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
        {
            if (keyData == Keys.F5)
            {
                if(LINK_SERVER!="")
                    CapNhatThongTinDuong(LINK_SERVER);
                return true;
            }
            return base.ProcessCmdKey(ref msg, keyData);
        }

/**********************************************************************************/
/**********************************************************************************/
// Star defining private and public functions

        //______________________________________ => Phan biet doan ma cua tung thanh vien

        //=========================================== => Phan biet cac ham
        /// <summary>
        /// Cập nhật lại thông tin đường.
        /// </summary>
        /// <param name="sLinkServer">Link server của thuê bao được chọn.</param>
        public void CapNhatThongTinDuong(string sLinkServer)
        {
            try
            {

                this.cbDistrict1.cbDist.DataSource = null;
                this.cbProvince1.cbProv.DataSource = null;
                this.cbStreet1.cbStrtName.DataSource = null;
                this.cbWard1.cbWardName.DataSource = null;
                
                // Trong An 02/04/2009
                // DUONG
                DataTable dt = new CMS_GYM.Street().dt_GetStreet();
                if (dt == null) return;
                this.cbStreet1.cbStrtName.DataSource = new DataView(dt);
                this.cbStreet1.cbStrtName.DisplayMember = dt.Columns[1].ColumnName;
                this.cbStreet1.cbStrtName.ValueMember = dt.Columns[0].ColumnName;
                // TINH THANH
                dt = null;
                dt = new CMS_GYM.Province().dt_GetProvince();
                if (dt == null) return;
                this.cbProvince1.cbProv.DataSource = new DataView(dt);
                this.cbProvince1.cbProv.DisplayMember = dt.Columns[1].ColumnName;
                this.cbProvince1.cbProv.ValueMember = dt.Columns[0].ColumnName;
                // QUAN HUYEN
                dt = null;
                if (this.cbDistrict1.Prov_ID != null)
                    dt = new CMS_GYM.District().dt_getDistrict(this.cbDistrict1.Prov_ID);
                else
                    dt = new CMS_GYM.District().getDistrict();
                if (dt == null || dt.Rows.Count == 0) return;
                this.cbDistrict1.cbDist.DataSource = new DataView(dt);
                this.cbDistrict1.cbDist.DisplayMember = dt.Columns[2].ColumnName;
                this.cbDistrict1.cbDist.ValueMember = dt.Columns[0].ColumnName;
                // PHUONG
                dt = null;
                if (this.cbWard1.Dist_ID != null)
                    dt =  new CMS_GYM.Ward().dt_getWards(this.cbWard1.Dist_ID);
                else
                    dt = new CMS_GYM.Ward().dt_getWard();
                if (dt == null) return;
                this.cbWard1.cbWardName.DataSource = new DataView(dt);
                this.cbWard1.cbWardName.DisplayMember = dt.Columns[2].ColumnName;
                this.cbWard1.cbWardName.ValueMember = dt.Columns[0].ColumnName;
                // end
            }
            catch (System.Exception)
            {
            	
            }
            
        }

        //_________________________________________________________

// End defining private and public functions
/**********************************************************************************/
    }
}

